#include <linux/module.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/genhd.h>
#include <linux/hdreg.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/blkdev.h>
#include <linux/blkpg.h>
#include <linux/delay.h>
#include <linux/io.h>

#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/dma.h>

#define RAMBLOCK_SIZE (1024*1024)

static DEFINE_SPINLOCK(ramblock_lock);


static struct request_queue *ramblock_queue;
static struct gendisk *ramblock_disk;
static unsigned char *ramblock_buf;
static int major;

static int ramblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
	geo->heads     = 2;
	geo->sectors   = 32;
	geo->cylinders = RAMBLOCK_SIZE/2/32/512;
	return 0;
}

static struct block_device_operations ramblock_fops = {
	.owner	= THIS_MODULE,
	.getgeo = ramblock_getgeo,
};

static void do_ramblock_queue(request_queue_t * q)
{
	struct request *req;
	
	while ((req = elv_next_request(q)) != NULL)
	{
		unsigned long offset = req->sector << 9;
		unsigned long len  = req->current_nr_sectors << 9;

		if(rq_data_dir(req) == READ)
		{
			memcpy(req->buffer, ramblock_buf+offset, len);
		}
		else
		{
			memcpy(ramblock_buf+offset, req->buffer, len);
		}
		end_request(req, 1);
	}
}

static int ramblock_init(void)
{
	ramblock_disk = alloc_disk(16);

	major = register_blkdev(0, "ramblock");
	
	ramblock_queue = blk_init_queue(do_ramblock_queue, &ramblock_lock);
	ramblock_disk->queue = ramblock_queue;

	ramblock_disk->major = major;
	ramblock_disk->first_minor = 0;
	sprintf(ramblock_disk->disk_name, "ramblock");
	ramblock_disk->fops = &ramblock_fops;
	set_capacity(ramblock_disk, RAMBLOCK_SIZE);

	ramblock_buf = kzalloc(RAMBLOCK_SIZE, GFP_KERNEL);

	add_disk(ramblock_disk);
	return 0;
}

static void ramblock_exit(void)
{
	unregister_blkdev(major, "ramblock");
	del_gendisk(ramblock_disk);
    put_disk(ramblock_disk);
    blk_cleanup_queue(ramblock_queue);

	kfree(ramblock_buf);
}

module_init(ramblock_init);
module_exit(ramblock_exit);

MODULE_LICENSE("GPL");




